package com.lord.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lord.exception.WeChatException;
import com.lord.utils.Result;
import com.lord.utils.ResultUtil;


/**
 * @Description 异常捕获
 * @author WangShiCong
 * @since 2017-4-14
 */
@ControllerAdvice
public class ExceptionHandle {
	
	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); 

	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result<?> handle(Exception e){
		if(e instanceof WeChatException){
			WeChatException weChatException = (WeChatException) e;
			return ResultUtil.error(weChatException.getCode(), weChatException.getMessage());
		}else{
			logger.error("【系统异常】 {}", e);
			return ResultUtil.error(-1, "未知错误");
		}
	}
}
